package c.c.b.b.b;

import android.content.Context;
import android.location.Address;
import android.net.Uri;
import android.os.Build;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import b.i.b.f;
import c.c.b.b.d.m;
import c.c.b.b.f.i;
import c.c.b.b.f.p;
import c.c.b.b.g.d;
import c.c.b.b.g.e;
import com.huawei.hms.android.HwBuildEx.VersionCodes;
import com.huawei.updatesdk.service.otaupdate.UpdateStatusCode.DialogButton;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
import org.json.JSONObject;

/* compiled from: AppConfig */
public class a implements c.c.b.b.e.a, c.c.b.b.g.e.a {
    public static a j;
    public final boolean a;
    public boolean b = true;
    public boolean c = false;
    public long d = 0;
    public long e = 0;
    public AtomicBoolean f = new AtomicBoolean(false);
    public final Context g;
    public volatile boolean h = false;
    public final e i = new e(Looper.getMainLooper(), this);

    /* compiled from: AppConfig */
    public class a implements Runnable {
        public void run() {
            a.this.f();
        }
    }

    /* compiled from: AppConfig */
    public class b implements Runnable {
        public final /* synthetic */ boolean a;

        public b(boolean z) {
            this.a = z;
        }

        public void run() {
            a aVar = a.this;
            boolean z = this.a;
            Objects.requireNonNull(aVar);
            c.c.b.b.g.b.a("TNCManager", "doRefresh, actual request");
            aVar.i();
            aVar.c = true;
            if (z) {
                try {
                    String[] f = c.c.b.b.c.a().f();
                    if (f == null || f.length <= 0) {
                        f = new String[0];
                    }
                    if (f.length != 0) {
                        aVar.b(0);
                        return;
                    }
                    return;
                } catch (Exception unused) {
                    aVar.f.set(false);
                    return;
                }
            }
            aVar.i.sendEmptyMessage(102);
        }
    }

    /* compiled from: AppConfig */
    public class c implements c.c.b.b.f.p.a<JSONObject> {
        public final /* synthetic */ int a;

        public c(int i) {
            this.a = i;
        }

        public void a(p<JSONObject> pVar) {
            JSONObject jSONObject = (JSONObject) pVar.a;
            if (jSONObject == null) {
                a.this.b(this.a + 1);
                return;
            }
            Object obj = null;
            try {
                obj = jSONObject.getString("message");
            } catch (Exception unused) {
            }
            if ("success".equals(obj)) {
                try {
                    if (a.d(a.this, jSONObject)) {
                        e eVar = a.this.i;
                        if (eVar != null) {
                            eVar.sendEmptyMessage(DialogButton.CONFIRM);
                        }
                    } else {
                        a.this.b(this.a + 1);
                    }
                } catch (Exception unused2) {
                }
                return;
            }
            a.this.b(this.a + 1);
        }

        public void b(p<JSONObject> pVar) {
            a.this.b(this.a + 1);
        }
    }

    public a(Context context, boolean z) {
        this.g = context;
        this.a = z;
    }

    public static a a(Context context) {
        a aVar;
        synchronized (a.class) {
            if (j == null) {
                a aVar2 = new a(context.getApplicationContext(), c.c.b.b.g.c.a(context));
                j = aVar2;
                c.c.b.b.c.d = aVar2;
            }
            aVar = j;
        }
        return aVar;
    }

    /* JADX WARNING: Removed duplicated region for block: B:54:0x0129 A:{Catch:{ all -> 0x019b, all -> 0x01c6 }} */
    /* JADX WARNING: Removed duplicated region for block: B:53:0x011e A:{Catch:{ all -> 0x019b, all -> 0x01c6 }} */
    /* JADX WARNING: Removed duplicated region for block: B:59:0x0145 A:{Catch:{ all -> 0x019b, all -> 0x01c6 }} */
    /* JADX WARNING: Removed duplicated region for block: B:58:0x0142 A:{Catch:{ all -> 0x019b, all -> 0x01c6 }} */
    /* JADX WARNING: Removed duplicated region for block: B:70:0x0175 A:{SYNTHETIC, Splitter:B:70:0x0175} */
    /* JADX WARNING: Removed duplicated region for block: B:62:0x0155  */
    /* JADX WARNING: Missing block: B:5:0x0023, code skipped:
            if ("success".equals(r0.getString("message")) == false) goto L_0x002b;
     */
    public static boolean d(c.c.b.b.b.a r10, java.lang.Object r11) throws java.lang.Exception {
        /*
        java.util.Objects.requireNonNull(r10);
        r0 = r11 instanceof java.lang.String;
        r1 = 1;
        r2 = 0;
        if (r0 == 0) goto L_0x0026;
    L_0x0009:
        r11 = (java.lang.String) r11;
        r0 = android.text.TextUtils.isEmpty(r11);
        if (r0 == 0) goto L_0x0012;
    L_0x0011:
        goto L_0x002b;
    L_0x0012:
        r0 = new org.json.JSONObject;
        r0.<init>(r11);
        r11 = "message";
        r11 = r0.getString(r11);
        r3 = "success";
        r11 = r3.equals(r11);
        if (r11 != 0) goto L_0x0029;
    L_0x0025:
        goto L_0x002b;
    L_0x0026:
        r0 = r11;
        r0 = (org.json.JSONObject) r0;
    L_0x0029:
        if (r0 != 0) goto L_0x002e;
    L_0x002b:
        r1 = 0;
        goto L_0x01e7;
    L_0x002e:
        r11 = "data";
        r11 = r0.getJSONObject(r11);
        monitor-enter(r10);
        r0 = r10.g;	 Catch:{ all -> 0x01e8 }
        r3 = "ss_app_config";
        r0 = r0.getSharedPreferences(r3, r2);	 Catch:{ all -> 0x01e8 }
        r0 = r0.edit();	 Catch:{ all -> 0x01e8 }
        r3 = java.lang.System.currentTimeMillis();	 Catch:{ all -> 0x01e8 }
        r5 = "last_refresh_time";
        r0.putLong(r5, r3);	 Catch:{ all -> 0x01e8 }
        r0.apply();	 Catch:{ all -> 0x01e8 }
        monitor-exit(r10);	 Catch:{ all -> 0x01e8 }
        r10 = c.c.b.b.e.f.a();
        r10 = r10.d;
        if (r10 == 0) goto L_0x01e7;
    L_0x0056:
        r10 = c.c.b.b.e.f.a();
        r10 = r10.d;
        r0 = c.c.b.b.e.e.d;
        r3 = r10.c;
        if (r3 != 0) goto L_0x006b;
    L_0x0062:
        r10 = "TNCConfigHandler";
        r11 = "handleConfigChanged: no mainProc";
        c.c.b.b.g.b.a(r10, r11);
        goto L_0x01e7;
    L_0x006b:
        r3 = c.c.b.b.e.f.a();
        r3 = r3.n;
        r3.clear();
        r3 = "";
        r4 = "ttnet_url_dispatcher_enabled";
        r4 = r11.optInt(r4, r2);	 Catch:{ all -> 0x019b }
        if (r4 <= 0) goto L_0x0080;
    L_0x007e:
        r4 = 1;
        goto L_0x0081;
    L_0x0080:
        r4 = 0;
    L_0x0081:
        r5 = "ttnet_dispatch_actions";
        r5 = r11.optJSONArray(r5);	 Catch:{ all -> 0x019b }
        r6 = c.c.b.b.c.c;	 Catch:{ all -> 0x019b }
        if (r6 == 0) goto L_0x00f6;
    L_0x008b:
        if (r4 == 0) goto L_0x00f6;
    L_0x008d:
        if (r5 == 0) goto L_0x00f6;
    L_0x008f:
        r4 = new java.util.ArrayList;	 Catch:{ all -> 0x019b }
        r4.<init>();	 Catch:{ all -> 0x019b }
        r6 = 0;
    L_0x0095:
        r7 = r5.length();	 Catch:{ all -> 0x019b }
        if (r6 >= r7) goto L_0x00c3;
    L_0x009b:
        r7 = r5.get(r6);	 Catch:{ all -> 0x019b }
        r7 = (org.json.JSONObject) r7;	 Catch:{ all -> 0x019b }
        r8 = "param";
        r7 = r7.getJSONObject(r8);	 Catch:{ all -> 0x019b }
        r8 = "service_name";
        r9 = "";
        r8 = r7.optString(r8, r9);	 Catch:{ all -> 0x019b }
        r9 = "idc_selection";
        r8 = r8.equals(r9);	 Catch:{ all -> 0x019b }
        if (r8 == 0) goto L_0x00c0;
    L_0x00b7:
        r8 = "strategy_info";
        r7 = r7.getJSONObject(r8);	 Catch:{ all -> 0x019b }
        r4.add(r7);	 Catch:{ all -> 0x019b }
    L_0x00c0:
        r6 = r6 + 1;
        goto L_0x0095;
    L_0x00c3:
        r5 = r4.isEmpty();	 Catch:{ all -> 0x019b }
        if (r5 != 0) goto L_0x00f6;
    L_0x00c9:
        r5 = new org.json.JSONObject;	 Catch:{ all -> 0x019b }
        r5.<init>();	 Catch:{ all -> 0x019b }
        r4 = r4.iterator();	 Catch:{ all -> 0x019b }
    L_0x00d2:
        r6 = r4.hasNext();	 Catch:{ all -> 0x019b }
        if (r6 == 0) goto L_0x00f7;
    L_0x00d8:
        r6 = r4.next();	 Catch:{ all -> 0x019b }
        r6 = (org.json.JSONObject) r6;	 Catch:{ all -> 0x019b }
        r7 = r6.keys();	 Catch:{ all -> 0x019b }
    L_0x00e2:
        r8 = r7.hasNext();	 Catch:{ all -> 0x019b }
        if (r8 == 0) goto L_0x00d2;
    L_0x00e8:
        r8 = r7.next();	 Catch:{ all -> 0x019b }
        r8 = (java.lang.String) r8;	 Catch:{ all -> 0x019b }
        r9 = r6.getString(r8);	 Catch:{ all -> 0x019b }
        r5.put(r8, r9);	 Catch:{ all -> 0x019b }
        goto L_0x00e2;
    L_0x00f6:
        r5 = 0;
    L_0x00f7:
        r4 = "tnc_config";
        r11 = r11.optJSONObject(r4);	 Catch:{ all -> 0x019b }
        if (r11 != 0) goto L_0x011c;
    L_0x00ff:
        if (r5 != 0) goto L_0x011c;
    L_0x0101:
        r4 = "TNCConfigHandler";
        r5 = " tnc host_replace_map config is null";
        r6 = c.c.b.b.g.b.d.a;	 Catch:{ all -> 0x019b }
        r7 = r6.a;	 Catch:{ all -> 0x019b }
        r8 = c.c.b.b.g.b.b.ERROR;	 Catch:{ all -> 0x019b }
        r7 = r7.compareTo(r8);	 Catch:{ all -> 0x019b }
        if (r7 > 0) goto L_0x0130;
    L_0x0111:
        r6 = r6.b;	 Catch:{ all -> 0x019b }
        r6 = (c.c.b.b.g.a) r6;	 Catch:{ all -> 0x019b }
        java.util.Objects.requireNonNull(r6);	 Catch:{ all -> 0x019b }
        android.util.Log.e(r4, r5);	 Catch:{ all -> 0x019b }
        goto L_0x0130;
    L_0x011c:
        if (r11 != 0) goto L_0x0129;
    L_0x011e:
        r11 = new org.json.JSONObject;	 Catch:{ all -> 0x019b }
        r11.<init>();	 Catch:{ all -> 0x019b }
        r4 = "host_replace_map";
        r11.put(r4, r5);	 Catch:{ all -> 0x019b }
        goto L_0x0130;
    L_0x0129:
        if (r5 == 0) goto L_0x0130;
    L_0x012b:
        r4 = "host_replace_map";
        r11.put(r4, r5);	 Catch:{ all -> 0x019b }
    L_0x0130:
        r4 = r10.b(r11);	 Catch:{ all -> 0x019b }
        r5 = "TNCConfigHandler";
        r6 = new java.lang.StringBuilder;	 Catch:{ all -> 0x019b }
        r6.<init>();	 Catch:{ all -> 0x019b }
        r7 = "handleConfigChanged, newConfig: ";
        r6.append(r7);	 Catch:{ all -> 0x019b }
        if (r4 != 0) goto L_0x0145;
    L_0x0142:
        r7 = "null";
        goto L_0x0149;
    L_0x0145:
        r7 = r4.toString();	 Catch:{ all -> 0x019b }
    L_0x0149:
        r6.append(r7);	 Catch:{ all -> 0x019b }
        r6 = r6.toString();	 Catch:{ all -> 0x019b }
        c.c.b.b.g.b.a(r5, r6);	 Catch:{ all -> 0x019b }
        if (r4 != 0) goto L_0x0175;
    L_0x0155:
        monitor-enter(r0);
        r11 = r10.a;	 Catch:{ all -> 0x0172 }
        r4 = "ttnet_tnc_config";
        r11 = r11.getSharedPreferences(r4, r2);	 Catch:{ all -> 0x0172 }
        r11 = r11.edit();	 Catch:{ all -> 0x0172 }
        r2 = "tnc_config_str";
        r11 = r11.putString(r2, r3);	 Catch:{ all -> 0x0172 }
        r11.apply();	 Catch:{ all -> 0x0172 }
        r10 = r10.a;	 Catch:{ all -> 0x0172 }
        b.i.b.f.o(r10, r1, r3);	 Catch:{ all -> 0x0172 }
        monitor-exit(r0);	 Catch:{ all -> 0x0172 }
        goto L_0x01e7;
    L_0x0172:
        r10 = move-exception;
        monitor-exit(r0);	 Catch:{ all -> 0x0172 }
        throw r10;
    L_0x0175:
        r10.b = r4;	 Catch:{ all -> 0x019b }
        r11 = r11.toString();	 Catch:{ all -> 0x019b }
        monitor-enter(r0);
        r3 = r10.a;	 Catch:{ all -> 0x0198 }
        r4 = "ttnet_tnc_config";
        r2 = r3.getSharedPreferences(r4, r2);	 Catch:{ all -> 0x0198 }
        r2 = r2.edit();	 Catch:{ all -> 0x0198 }
        r3 = "tnc_config_str";
        r2 = r2.putString(r3, r11);	 Catch:{ all -> 0x0198 }
        r2.apply();	 Catch:{ all -> 0x0198 }
        r10 = r10.a;	 Catch:{ all -> 0x0198 }
        b.i.b.f.o(r10, r1, r11);	 Catch:{ all -> 0x0198 }
        monitor-exit(r0);	 Catch:{ all -> 0x0198 }
        goto L_0x01e7;
    L_0x0198:
        r10 = move-exception;
        monitor-exit(r0);	 Catch:{ all -> 0x0198 }
        throw r10;
    L_0x019b:
        r11 = move-exception;
        r11.printStackTrace();	 Catch:{ all -> 0x01c6 }
        r11 = new c.c.b.b.e.d;	 Catch:{ all -> 0x01c6 }
        r11.m996init();	 Catch:{ all -> 0x01c6 }
        r10.b = r11;	 Catch:{ all -> 0x01c6 }
        monitor-enter(r0);
        r11 = r10.a;	 Catch:{ all -> 0x01c3 }
        r4 = "ttnet_tnc_config";
        r11 = r11.getSharedPreferences(r4, r2);	 Catch:{ all -> 0x01c3 }
        r11 = r11.edit();	 Catch:{ all -> 0x01c3 }
        r2 = "tnc_config_str";
        r11 = r11.putString(r2, r3);	 Catch:{ all -> 0x01c3 }
        r11.apply();	 Catch:{ all -> 0x01c3 }
        r10 = r10.a;	 Catch:{ all -> 0x01c3 }
        b.i.b.f.o(r10, r1, r3);	 Catch:{ all -> 0x01c3 }
        monitor-exit(r0);	 Catch:{ all -> 0x01c3 }
        goto L_0x01e7;
    L_0x01c3:
        r10 = move-exception;
        monitor-exit(r0);	 Catch:{ all -> 0x01c3 }
        throw r10;
    L_0x01c6:
        r11 = move-exception;
        monitor-enter(r0);
        r4 = r10.a;	 Catch:{ all -> 0x01e4 }
        r5 = "ttnet_tnc_config";
        r2 = r4.getSharedPreferences(r5, r2);	 Catch:{ all -> 0x01e4 }
        r2 = r2.edit();	 Catch:{ all -> 0x01e4 }
        r4 = "tnc_config_str";
        r2 = r2.putString(r4, r3);	 Catch:{ all -> 0x01e4 }
        r2.apply();	 Catch:{ all -> 0x01e4 }
        r10 = r10.a;	 Catch:{ all -> 0x01e4 }
        b.i.b.f.o(r10, r1, r3);	 Catch:{ all -> 0x01e4 }
        monitor-exit(r0);	 Catch:{ all -> 0x01e4 }
        throw r11;
    L_0x01e4:
        r10 = move-exception;
        monitor-exit(r0);	 Catch:{ all -> 0x01e4 }
        throw r10;
    L_0x01e7:
        return r1;
    L_0x01e8:
        r11 = move-exception;
        monitor-exit(r10);	 Catch:{ all -> 0x01e8 }
        throw r11;
        */
        throw new UnsupportedOperationException("Method not decompiled: c.c.b.b.b.a.d(c.c.b.b.b.a, java.lang.Object):boolean");
    }

    public final void b(int i) {
        String[] f = c.c.b.b.c.a().f();
        if (f == null || f.length <= 0) {
            f = new String[0];
        }
        if (f.length <= i) {
            g(102);
            return;
        }
        CharSequence charSequence = f[i];
        if (TextUtils.isEmpty(charSequence)) {
            g(102);
            return;
        }
        try {
            String e = e(charSequence);
            if (TextUtils.isEmpty(e)) {
                g(102);
                return;
            }
            m mVar = new m(0, e, new JSONObject(), new c(i));
            i iVar = new i();
            iVar.a = VersionCodes.CUR_DEVELOPMENT;
            iVar.c = 0;
            mVar.setRetryPolicy(iVar).build(c.c.b.b.c.b(this.g));
        } catch (Throwable th) {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("try app config exception: ");
            stringBuilder.append(th);
            c.c.b.b.g.b.a("AppConfig", stringBuilder.toString());
        }
    }

    public synchronized void c(boolean z) {
        if (this.a) {
            if (!this.c) {
                if (this.b) {
                    this.b = false;
                    this.d = 0;
                    this.e = 0;
                }
                long j = z ? 10800000 : 43200000;
                long currentTimeMillis = System.currentTimeMillis();
                if (currentTimeMillis - this.d > j && currentTimeMillis - this.e > 120000) {
                    z = f.w(this.g);
                    if (!this.h || z) {
                        h(z);
                    }
                }
            }
        } else if (this.d <= 0) {
            try {
                com.bytedance.sdk.openadsdk.j.e.a().execute(new a());
            } catch (Throwable unused) {
            }
        }
    }

    public final String e(String str) {
        if (TextUtils.isEmpty(str)) {
            return null;
        }
        StringBuilder j = c.b.a.a.a.j("https://", str, "/get_domains/v4/");
        Address a = c.c.b.b.c.a().a(this.g);
        d dVar = new d(j.toString());
        if (a != null && a.hasLatitude() && a.hasLongitude()) {
            dVar.a("latitude", a.getLatitude());
            dVar.a("longitude", a.getLongitude());
            str = a.getLocality();
            if (!TextUtils.isEmpty(str)) {
                dVar.c("city", Uri.encode(str));
            }
        }
        try {
            dVar.c("abi", Build.SUPPORTED_ABIS[0]);
        } catch (Throwable th) {
            th.printStackTrace();
        }
        dVar.b("aid", c.c.b.b.c.a().a());
        dVar.c("device_platform", c.c.b.b.c.a().c());
        dVar.c("channel", c.c.b.b.c.a().b());
        dVar.b("version_code", c.c.b.b.c.a().d());
        dVar.c("custom_info_1", c.c.b.b.c.a().e());
        return dVar.toString();
    }

    public synchronized void f() {
        if (System.currentTimeMillis() - this.d > 3600000) {
            this.d = System.currentTimeMillis();
            try {
                if (c.c.b.b.e.f.a().d != null) {
                    c.c.b.b.e.f.a().d.c();
                }
            } catch (Exception unused) {
            }
        }
    }

    public final void g(int i) {
        e eVar = this.i;
        if (eVar != null) {
            eVar.sendEmptyMessage(i);
        }
    }

    public boolean h(boolean z) {
        StringBuilder g = c.b.a.a.a.g("doRefresh: updating state ");
        g.append(this.f.get());
        String str = "TNCManager";
        c.c.b.b.g.b.a(str, g.toString());
        if (this.f.compareAndSet(false, true)) {
            if (z) {
                this.e = System.currentTimeMillis();
            }
            com.bytedance.sdk.openadsdk.j.e.a().execute(new b(z));
            return true;
        }
        c.c.b.b.g.b.a(str, "doRefresh, already running");
        return false;
    }

    /* JADX WARNING: Missing block: B:15:0x0038, code skipped:
            return;
     */
    public synchronized void i() {
        /*
        r5 = this;
        monitor-enter(r5);
        r0 = r5.h;	 Catch:{ all -> 0x0039 }
        if (r0 == 0) goto L_0x0007;
    L_0x0005:
        monitor-exit(r5);
        return;
    L_0x0007:
        r0 = 1;
        r5.h = r0;	 Catch:{ all -> 0x0039 }
        r0 = r5.g;	 Catch:{ all -> 0x0039 }
        r1 = "ss_app_config";
        r2 = 0;
        r0 = r0.getSharedPreferences(r1, r2);	 Catch:{ all -> 0x0039 }
        r1 = "last_refresh_time";
        r2 = 0;
        r0 = r0.getLong(r1, r2);	 Catch:{ all -> 0x0039 }
        r2 = java.lang.System.currentTimeMillis();	 Catch:{ all -> 0x0039 }
        r4 = (r0 > r2 ? 1 : (r0 == r2 ? 0 : -1));
        if (r4 <= 0) goto L_0x0024;
    L_0x0023:
        r0 = r2;
    L_0x0024:
        r5.d = r0;	 Catch:{ all -> 0x0039 }
        r0 = c.c.b.b.e.f.a();	 Catch:{ all -> 0x0039 }
        r0 = r0.d;	 Catch:{ all -> 0x0039 }
        if (r0 == 0) goto L_0x0037;
    L_0x002e:
        r0 = c.c.b.b.e.f.a();	 Catch:{ all -> 0x0039 }
        r0 = r0.d;	 Catch:{ all -> 0x0039 }
        r0.a();	 Catch:{ all -> 0x0039 }
    L_0x0037:
        monitor-exit(r5);
        return;
    L_0x0039:
        r0 = move-exception;
        monitor-exit(r5);
        throw r0;
        */
        throw new UnsupportedOperationException("Method not decompiled: c.c.b.b.b.a.i():void");
    }

    public void a(Message message) {
        int i = message.what;
        String str = "TNCManager";
        if (i == DialogButton.CONFIRM) {
            this.c = false;
            this.d = System.currentTimeMillis();
            c.c.b.b.g.b.a(str, "doRefresh, succ");
            if (this.b) {
                c(false);
            }
            this.f.set(false);
        } else if (i == 102) {
            this.c = false;
            if (this.b) {
                c(false);
            }
            c.c.b.b.g.b.a(str, "doRefresh, error");
            this.f.set(false);
        }
    }
}
